package com.zzj.utils;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;

public class QueryUtil {


    /**
     * 与
     */
    public static <T> void andParams(QueryWrapper<T> queryWrapper, String andString) {
        if (andString == null) {
            return;
        }
        JSONObject andParams = JSONObject.parseObject(andString);
        if (andParams != null && !andParams.isEmpty()) {
            for (Map.Entry<String, Object> entry : andParams.entrySet()) {
                if (Objects.equals(entry.getValue(), "")) continue;
                String value = entry.getValue().toString();
                if (value.startsWith("%") && value.endsWith("%")) {
                    queryWrapper.like(StrUtil.toUnderlineCase(entry.getKey()), value);
                } else if (value.startsWith("%")) {
                    queryWrapper.likeLeft(StrUtil.toUnderlineCase(entry.getKey()), value);
                } else if (value.endsWith("%")) {
                    queryWrapper.likeRight(StrUtil.toUnderlineCase(entry.getKey()), value);
                } else {
                    queryWrapper.eq(StrUtil.toUnderlineCase(entry.getKey()), value);
                }

            }
        }
    }


    /**
     * 或
     */
    public static <T> void orParams(QueryWrapper<T> queryWrapper, String orString) {
        if (orString == null) {
            return;
        }
        JSONObject orParams = JSONObject.parseObject(orString);
        if (!orParams.isEmpty()) {
            boolean isFlag = false;
            for (Map.Entry<String, Object> entry : orParams.entrySet()) {
                if (!Objects.equals(entry.getValue(), "")) {
                    isFlag = true;
                    break;
                }
            }
            Consumer<QueryWrapper<T>> wrapperConsumer = qw -> {
                for (Map.Entry<String, Object> entry : orParams.entrySet()) {
                    String value = entry.getValue().toString();
                    if (Objects.equals(value, "")) continue;
                    if (value.startsWith("%") && value.endsWith("%")) {
                        qw.like(StrUtil.toUnderlineCase(entry.getKey()), value);
                    } else if (value.startsWith("%")) {
                        qw.likeLeft(StrUtil.toUnderlineCase(entry.getKey()), value);
                    } else if (value.endsWith("%")) {
                        qw.likeRight(StrUtil.toUnderlineCase(entry.getKey()), value);
                    } else {
                        qw.eq(StrUtil.toUnderlineCase(entry.getKey()), value);
                    }
                    // 在最后一次循环结束后删除最后一个OR条件
                    if (!entry.equals(orParams.entrySet().iterator().next())) {
                        qw.or();
                    }
                }
            };
            if (isFlag) {
                queryWrapper.and(wrapperConsumer);
            }
        }
    }

    /**
     * 排序
     */
    public static <T> void orderParams(QueryWrapper<T> queryWrapper, String orderString) {
        if (orderString == null) {
            return;
        }
        JSONObject orderParams = JSONObject.parseObject(orderString);
        if (orderParams != null && !orderParams.isEmpty()) {
            for (Map.Entry<String, Object> entry : orderParams.entrySet()) {
                queryWrapper.orderBy(!Objects.equals(entry.getValue(), ""), Objects.equals(entry.getValue(), "1"), StrUtil.toUnderlineCase(entry.getKey()));
            }
        }
    }


    /**
     * 查询条件封装包
     *
     * @param andString
     * @param orString
     * @param orderString
     * @param <T>
     * @return
     */
    public static <T> QueryWrapper<T> paramsWrapper(String andString, String orString, String orderString) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        andParams(queryWrapper, andString);
        orParams(queryWrapper, orString);
        orderParams(queryWrapper, orderString);
        return queryWrapper;
    }
}
